<?php

define('THEME_URL', get_template_directory_uri());

// Fillter Search
// function searchfilter($query) {

// if ($query->is_search && !is_admin() ) {
// $query->set('post_type',array('product'));
// }
// return $query;
// }
// add_filter('pre_get_posts','searchfilter');

// Class Menu Walker
class add_span_walker extends Walker_Nav_Menu
{
    function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0)
    {
        global $wp_query;
        $indent = ($depth) ? str_repeat("\t", $depth) : '';

        $class_names = '';

        $classes = empty($item->classes) ? array() : (array)$item->classes;
        $classes[] = 'menu-item-' . $item->ID;

        $class_names = join(' ', apply_filters('nav_menu_css_class', array_filter($classes), $item, $args));
        $class_names = $class_names ? ' class="' . esc_attr($class_names) . '"' : '';

        $id = apply_filters('nav_menu_item_id', 'menu-item-' . $item->ID, $item, $args);
        $id = $id ? ' id="' . esc_attr($id) . '"' : '';

        $output .= $indent . '<li' . $id . $class_names . '>';

        $attributes = !empty($item->attr_title) ? ' title="' . esc_attr($item->attr_title) . '"' : '';
        $attributes .= !empty($item->target) ? ' target="' . esc_attr($item->target) . '"' : '';
        $attributes .= !empty($item->xfn) ? ' rel="' . esc_attr($item->xfn) . '"' : '';
        $attributes .= !empty($item->url) ? ' href="' . esc_attr($item->url) . '"' : '';

        $item_output = $args->before;
        $item_output .= '<a' . $attributes . '>';
        $item_output .= $args->link_before . apply_filters('the_title', $item->title, $item->ID) . $args->link_after;

        if ('primary' == $args->theme_location) {
            $submenus = 0 == $depth || 1 == $depth ? get_posts(array('post_type' => 'nav_menu_item', 'numberposts' => 1, 'meta_query' => array(array('key' => '_menu_item_menu_item_parent', 'value' => $item->ID, 'fields' => 'ids')))) : false;
            $item_output .= !empty($submenus) ? (0 == $depth ? '<span class="arrow"></span>' : '<span class="sub-arrow"></span>') : '';
        }
        $item_output .= '</a>';
        $item_output .= $args->after;

        $output .= apply_filters('walker_nav_menu_start_el', $item_output, $item, $depth, $args);
    }
}


function __search_by_title_only($search, &$wp_query)
{
    global $wpdb;
    if (empty($search))
        return $search; // skip processing - no search term in query
    $q = $wp_query->query_vars;
    $n = !empty($q['exact']) ? '' : '%';
    $search =
    $searchand = '';
    foreach ((array)$q['search_terms'] as $term) {
        $term = esc_sql(like_escape($term));
        $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $searchand = ' AND ';
    }
    if (!empty($search)) {
        $search = " AND ({$search}) ";
        if (!is_user_logged_in())
            $search .= " AND ($wpdb->posts.post_password = '') ";
    }
    return $search;
}

add_filter('posts_search', '__search_by_title_only', 500, 2);


// Add image size
function azio_theme_setup()
{
    add_image_size('avatar', 80, 80, true); // 1x1
    add_theme_support('post-thumbnails');
}

add_action('after_setup_theme', 'azio_theme_setup');

// Excerpt length
function wpe_excerptlength_cat($length)
{
    return 80;
}

;
function wpe_excerptlength_sidebar($length)
{
    return 15;
}

;
function wpe_excerptlength_index($length)
{
    return 45;
}

;

function wpe_excerptmore($more)
{
    return '...';
}

;

function wpe_excerpt($length_callback = '', $more_callback = '')
{
    global $post;
    if (function_exists($length_callback)) {
        add_filter('excerpt_length', $length_callback);
    }
    if (function_exists($more_callback)) {
        add_filter('excerpt_more', $more_callback);
    }
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    echo $output;
}

// Alow html in term desc
function sb_allow_category_desc_html() {
    foreach ( array( 'pre_term_description', 'pre_link_description', 'pre_link_notes', 'pre_user_description' ) as $filter ) {
        remove_filter( $filter, 'wp_filter_kses' );
    }
    foreach ( array( 'term_description', 'link_description', 'link_notes', 'user_description' ) as $filter ) {
        remove_filter( $filter, 'wp_kses_data' );
    }
}
add_action( 'init', 'sb_allow_category_desc_html' );


function az_modify_num_posts_for_product($query)
{
//    if ($query->is_main_query() && $query->is_page('danh-gia') && !is_admin()){
//        var_dump($query);
//    }
}

add_action('pre_get_posts', 'az_modify_num_posts_for_product');


function azio_scripts_styles()
{
    $version = '19102013_0830';

    // enqueue style

    wp_enqueue_style('az-style-theme', THEME_URL . '/style.css', array('az-reset'), $version);

    wp_enqueue_style('az-reset', THEME_URL . '/css/reset.css', array(), $version);
    wp_enqueue_style('az-style', THEME_URL . '/css/style.css', array('az-reset'), $version);
    wp_enqueue_style('az-responsive-leyouts', THEME_URL . '/css/responsive-leyouts.css', array('az-reset'), $version, 'screen');
    wp_enqueue_style('az-color-switcher', THEME_URL . '/js/style-switcher/color-switcher.css', array('az-reset'), $version, 'screen');
    wp_enqueue_style('az-skin', THEME_URL . '/js/jcarousel/skin.css"', array('az-reset'), $version);
    wp_enqueue_style('az-fullwidth', THEME_URL . '/js/revolutionslider/css/fullwidth.css', array('az-reset'), $version, 'screen');
    wp_enqueue_style('az-settings', THEME_URL . '/js/revolutionslider/rs-plugin/css/settings.css', array('az-reset'), $version, 'screen');
    wp_enqueue_style('az-accordion', THEME_URL . '/js/accordion/accordion.css', array('az-reset'), $version);


    // enqueue script
    wp_enqueue_script('az-ddsmoothmenu', THEME_URL . '/js/mainmenu/ddsmoothmenu.js', array('jquery'), $version, true);
    wp_enqueue_script('az-selectnav', THEME_URL . '/js/mainmenu/selectnav.js', array('jquery'), $version, true);
    wp_enqueue_script('az-jcarousel', THEME_URL . '/js/jcarousel/jquery.jcarousel.min.js', array('jquery'), $version, true);

    wp_enqueue_script('az-revolution-plugins', THEME_URL . '/js/revolutionslider/rs-plugin/js/jquery.themepunch.plugins.min.js', array('jquery'), $version, true);
    wp_enqueue_script('az-revolution', THEME_URL . '/js/revolutionslider/rs-plugin/js/jquery.themepunch.revolution.min.js', array('jquery'), $version, true);

    wp_enqueue_script('az-mainmenu-scripts', THEME_URL . '/js/mainmenu/scripts.js', array('jquery'), $version, true);
    wp_enqueue_script('az-accordion-custom', THEME_URL . '/js/accordion/custom.js', array('jquery'), $version, true);


    // Load JS Ajax
    wp_enqueue_script('azio-ajax-handle', THEME_URL . '/js/ajax/azio-ajax.js', array('jquery'), $version, true);
    wp_localize_script('azio-ajax-handle', 'azio_ajax', array('ajaxurl' => admin_url('admin-ajax.php')));

}

add_action('wp_enqueue_scripts', 'azio_scripts_styles');

